<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maximumSum = function (arr) {
      // sum表示不删除元素的最大值
      // deleteSum表示删除了某个元素的最大值
      let sum = new Array(arr.length);
      let deleteSum = new Array(arr.length);

      sum[0] = arr[0];
      deleteSum[0] = -Infinity;

      let res = Math.max(sum[0], deleteSum[0]);

      for (let i = 1; i < arr.length; i++) {
        sum[i] = Math.max(sum[i - 1] + arr[i], arr[i]);
        deleteSum[i] = Math.max(deleteSum[i - 1] + arr[i], sum[i - 1]); // 第二种情况是删除当前元素
        res = Math.max(res, sum[i], deleteSum[i]);
      }

      return res;
    };
    console.log(maximumSum(arr = [1, -2, 0, 3]))
  </script>
</body>

</html>